package com.bls.cate_s.servlet;

import com.bls.cate_s.model.Root;
import com.bls.cate_s.model.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

/**
 * http://localhost:8080/CateService/LoginServlet?account=bls&passwd=123&RequestType=Users
 * http://47.104.76.28:8080/CateService/LoginServlet?account=bls&passwd=123&RequestType=Users
 */

public class LoginServlet extends BaseServlet {

    public Root Users(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("account");
        String password = request.getParameter("passwd");
        ArrayList<User> users = new ArrayList<User>();

        Connection conn;
        Statement stmt;
        ResultSet rs;

        int id = 0;
        String name2 = "0";
        try {
            conn = DriverManager.getConnection("jdbc:sqlserver://localhost:8080;DatabaseName=CateData;", "sa", "BinglingSu");
            stmt = conn.createStatement();
            String sql = "select * from aa";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                id = rs.getInt("id");
                name2 = rs.getString("name");
                System.out.println("Sno:" + id + "\tSame:" + name2 + "\tSage:");
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }


        for (int i = 0; i < 10; i++) {
            User user = new User(i, "sa  " + id, "sa" + name2, "123");
            users.add(user);
        }
        return new Root<User>(false, 100, null, users, "123", 1, 1);
    }
}
